import { Vector2, BufferAttribute } from "three";
import { BufferGeometry } from "three/src/core/BufferGeometry";

export default class WidthLineGeometry extends BufferGeometry {    
    constructor(firstPoint: Vector2, secondPoint: Vector2, planeWidth: number, centerPoint?: Vector2) {
        super();

        let verticalVector = this.getVerticalVector(firstPoint, secondPoint); //获取垂直向量
        this.createGeometry(verticalVector, planeWidth, firstPoint, secondPoint, centerPoint);
    }

     /**
    * 获取垂直向量
    * @param firstPoint 点信息
    * @param secondPoint 点信息
    */
     private getVerticalVector(firstPoint: Vector2, secondPoint: Vector2) { //获取两个点构成向量的垂直向量
        let slope = 0; //斜率
        let verticalVector: Vector2;
        if (firstPoint.x - secondPoint.x == 0) {//完全垂直于X轴
            slope = 0;
            verticalVector = new Vector2(1, slope); //垂直于直线的向量
        } else {
            slope = (firstPoint.y - secondPoint.y) / (firstPoint.x - secondPoint.x);
            if (slope != 0) {
                slope = -1 / slope;
                verticalVector = new Vector2(1, slope); //垂直于直线的向量
            } else {//完全垂直于Y轴
                verticalVector = new Vector2(0, 1); //垂直于直线的向量
            }
        }

        verticalVector = verticalVector.multiplyScalar(1).normalize();//单位化垂直向量
        return verticalVector;
    }
    /**
    * 计算出几何体的顶点
    * @param verticalVector 线垂直向量
    * @param planeWidth 线缓冲区外延长度
    * @param firstPoint 线第一个点
    * @param secondPoint 线第二个点
    * @param centerPoint 线中心点
    */
    private createGeometry(verticalVector, planeWidth, firstPoint, secondPoint, centerPoint?) { //计算出几何体的顶点
        // const geometry = new BufferGeometry();

        let tempVerticalVector = new Vector2();
        let tempFirstPoint = new Vector2();
        let tempSecondPoint = new Vector2();

        if (centerPoint) {
            firstPoint = firstPoint.sub(centerPoint);
            secondPoint = secondPoint.sub(centerPoint);
        }

        let offsetVector = tempVerticalVector.copy(verticalVector);
        offsetVector.multiplyScalar(planeWidth);
        tempFirstPoint.copy(firstPoint);
        let pointOne = new Vector2().copy(tempFirstPoint.add(offsetVector));

        offsetVector = tempVerticalVector.copy(verticalVector);
        offsetVector.multiplyScalar(planeWidth * -1);
        tempFirstPoint.copy(firstPoint);
        let pointTwo = new Vector2().copy(tempFirstPoint.add(offsetVector));

        offsetVector = tempVerticalVector.copy(verticalVector);
        offsetVector.multiplyScalar(planeWidth);
        tempSecondPoint.copy(secondPoint);
        let pointThree = new Vector2().copy(tempSecondPoint.add(offsetVector));

        offsetVector = tempVerticalVector.copy(verticalVector);
        offsetVector.multiplyScalar(planeWidth * -1);
        tempSecondPoint.copy(secondPoint);
        let pointFour = new Vector2().copy(tempSecondPoint.add(offsetVector));

        // 定义顶点位置
        const vertices = new Float32Array([
            pointOne.x, pointOne.y, 0,
            pointTwo.x, pointTwo.y, 0,
            pointThree.x, pointThree.y, 0,
            pointFour.x, pointFour.y, 0
        ]);

        const indices = new Uint32Array([
            0, 1, 3,
            0, 3, 2
        ]);

        const uvs = new Float32Array([
            1, 1, 1, 0, 0, 0,
            1, 1, 0, 0, 0, 1
        ]);

        let pointArray = [pointOne, pointThree, pointFour, pointTwo];
        this.setAttribute('position', new BufferAttribute(vertices, 3));
        this.setIndex(new BufferAttribute(indices, 1));
        this.setAttribute('uv', new BufferAttribute(uvs, 2));
        this['pointArray'] = pointArray;
    }

}